package com.wxtl.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.wxtl.action.qo.PackQo;
import com.wxtl.action.vo.PackVo;
import com.wxtl.dao.CustomerDAO;
import com.wxtl.dao.DownLogDAO;
import com.wxtl.dao.UpgradePackDAO;
import com.wxtl.entity.Customer;
import com.wxtl.entity.DownLog;
import com.wxtl.entity.UpgradePack;
import com.wxtl.exception.ServiceException;
import com.wxtl.util.DataGridModel;
import com.wxtl.util.DateUtil;
import com.wxtl.util.Page;
import com.wxtl.ws.UpPackAction;
import com.wxtl.ws.qo.UpgradePackQo;

@Service
public class UpgradePackService {
	private UpgradePackDAO upgradePackDAO;
	private CustomerDAO customerDAO;
	private DownLogDAO downLogDAO;
	
	public void setUpgradePackDAO(UpgradePackDAO upgradePackDAO) {
		this.upgradePackDAO = upgradePackDAO;
	}

	public void setCustomerDAO(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	public void setDownLogDAO(DownLogDAO downLogDAO) {
		this.downLogDAO = downLogDAO;
	}

	public List<UpgradePack> queryPackList(UpgradePackQo packQo){
		//先做客户的验证 客户编号&授权编号&企业名称
//		Customer customer=customerDAO.queryByCustsn(packQo.getCustsn().trim());
//		if(customer==null)
//			throw new ServiceException("客户编号"+packQo.getCustsn()+"不存在！");
//		if(!packQo.getAuthsn().equals(customer.getAuthsn().trim()))
//			throw new ServiceException("客户授权编号"+packQo.getAuthsn()+"错误！");
//		if(!packQo.getEntname().equals(customer.getEntName().trim()))
//			throw new ServiceException("企业名称"+packQo.getEntname()+"输入错误");
		
		//解析产品代码&版本&修正号
		String prolist = packQo.getProlist();
		String[] pros=prolist.split(",");
		List<UpgradePack> upList=new ArrayList<UpgradePack>();
		for(int i=0;i<pros.length;i++){
			UpgradePack up=new UpgradePack();
			String[] ups=pros[i].split("\\|");
			up.setProCode(ups[0]);
			up.setSuptVersion(ups[1]);
			up.setSuptRevision(ups[2]);
			
			upList.add(up);
		}
		
		return upgradePackDAO.queryPackList(upList);
	}
	
	public UpgradePack downPack(com.wxtl.util.UPSProtocol upsp){
		//先做客户的验证 客户编号&授权编号&企业名称
		Customer customer=customerDAO.queryByCustsn(upsp.getCustsn().trim());
		if(customer==null)
			throw new ServiceException("客户编号"+upsp.getCustsn()+"不存在！");
		if(!upsp.getAuthsn().equals(customer.getAuthsn().trim()))
			throw new ServiceException("客户授权编号"+upsp.getAuthsn()+"错误！");
		
		UpgradePack pack=upgradePackDAO.getUpgradePackById(Integer.parseInt(upsp.getUpgradeId()));
		if(pack==null||pack.getDownpath()==null||pack.getDownpath().equals(""))
			throw new ServiceException("请求的编号为"+upsp.getUpCode()+"升级包不存在");
		
		//更新客户表
		customer.setMac(upsp.getMac());
		customer.setOs(upsp.getOs());
		if(upsp.getLastuptime()!=null)
			customer.setLastUptime(DateUtil.toTimestamp(DateUtil.parse(upsp.getLastuptime(),"yyyy-MM-dd HH:mm:ss")));
		customer.setLanip(upsp.getLanip());

		customer.setPort(Integer.parseInt(upsp.getPort()==null||upsp.getPort().equals("")?"0":upsp.getPort()));
		customer.setOther(customer.getOther()+"||"+ upsp.getOther());
		customerDAO.update(customer);
		
		//每次下载的时候应该创建新日志，而不是修改之前的日志
		DownLog dl = new DownLog();;//=downLogDAO.queryByLog(upsp.getCustsn(),Integer.valueOf(upsp.getUpgradeId()));
		/*if(dl==null){//不存在，则新建
			dl=new DownLog();
		}*/
		
		//记录下载日志
		dl.setCustsn(upsp.getCustsn());
		dl.setUpName(pack.getUpName());
		dl.setUpCode(pack.getUpCode()+"");
		dl.setVersion(pack.getSuptVersion());
		dl.setRevision(pack.getSuptRevision());
		dl.setUpgradeId(pack.getUpgradeId());
		dl.setDownTime(new Timestamp(new Date().getTime()));
		dl.setRequest(upsp.toString());//请求信息合集
		downLogDAO.saveOrUpdate(dl);//保存下载日志信息
		
		//更新UP表
		if(pack.getDownTotal()==null)
			pack.setDownTotal(0);
		pack.setDownTotal(pack.getDownTotal()+1);
		upgradePackDAO.saveOrUpdate(pack);
		
		return pack;
	}
	
	/**
	 * 报告下载日志
	 * @param upsp
	 */
	public void reportLog(UpPackAction upsp){
		
		DownLog dl=downLogDAO.queryByLog(upsp.getCustsn(), Integer.valueOf(upsp.getUpgradeId()));
		if(dl==null){//不存在，则新建
			dl=new DownLog();
		}
		System.out.println("UpgradePackService.java:update reporting........"+dl.getCustsn());
//		dl.setCustsn(upsp.getCustsn());
		dl.setInstallTime(DateUtil.toTimestamp(new Date()));
//		dl.setRevision(upsp.getRevision());
//		dl.setUpCode(upsp.getUpCode());
//		dl.setUpgradeId(Integer.valueOf(upsp.getUpgradeId()));
//		dl.setVersion(upsp.getVersion());
		downLogDAO.update(dl);//更新
	}
	
	public Page query(DataGridModel dgm,UpgradePack pack){
		return upgradePackDAO.query(dgm,pack);
	}
	
	public Page queryCount(DataGridModel dgm,PackQo packQo){
		Page page = upgradePackDAO.query(dgm,packQo);
		List<UpgradePack> ps = page.getRows();
		List<PackVo> pos = new ArrayList<PackVo>();
		
		for(int i=0;i<ps.size();i++){
			UpgradePack p = ps.get(i);
			PackVo packVo = new PackVo();
			packVo.setUpCode(p.getUpCode());
			packVo.setUpType(p.getUpType());
			packVo.setUpName(p.getUpName());
			packVo.setExVersion(p.getExVersion());
			packVo.setProCode(p.getProCode());
			packVo.setReleaseDate(p.getReleaseDate());
			packVo.setSuptVersion(p.getSuptVersion());
			packVo.setSuptRevision(p.getSuptRevision());
			packVo.setDownTotal(p.getDownTotal()==null?0:p.getDownTotal());
			//查询安装成功次数
			List list = downLogDAO.queryLogByCode(p.getUpCode());
			int count = 0;
			if(list!=null && list.size()>0){
				count = list.size();
			}
			packVo.setInstallTotal(count);
			pos.add(packVo);
		}
		page.setRows(pos);
		return page;		
	}
	
	public void delete(int[] ids){
		upgradePackDAO.delete(ids);
	}
	
	public UpgradePack getPackById(int id){
		return upgradePackDAO.getUpgradePackById(id);
	}
	
	public void add(UpgradePack upgradePack, String delFlag){
		if(delFlag!=null && delFlag.equals("on")){
			//废除上一版本的操作
			deleteByProCode(upgradePack.getProCode());
		}
		upgradePack.setReleaseDate(DateUtil.toTimestamp(new Date()));
		upgradePack.setIsPush(false);
		upgradePack.setDownTotal(0);
		upgradePackDAO.add(upgradePack);
	}
	
	public void update(UpgradePack upgradePack){
		upgradePack.setIsPush(false);
		upgradePackDAO.update(upgradePack);
	}
	
	public void deleteByProCode(String proCode){
		upgradePackDAO.deleteByProCode(new String[]{proCode});
	}
}
